<template>
  <div class="post-images-preview">
    <div v-if="!imageList || imageList.length === 0" class="no-image">
      <el-icon><Picture /></el-icon>
      <span>暂无图片</span>
    </div>
    <el-image 
      v-else
      :src="firstImageUrl" 
      fit="cover" 
      :style="`width:${realWidth};height:${realHeight};`"
      :preview-src-list="allImageUrls" 
      preview-teleported
      class="post-image"
    >
      <template #error>
        <div class="image-slot">
          <el-icon><picture-filled /></el-icon>
        </div>
      </template>
    </el-image>
    <div v-if="imageList && imageList.length > 1" class="image-count">
      +{{ imageList.length - 1 }}
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue';
import { isExternal } from "@/utils/validate";
import { Picture, PictureFilled } from '@element-plus/icons-vue';

const props = defineProps({
  images: {
    type: String,
    default: ''
  },
  width: {
    type: [Number, String],
    default: 60
  },
  height: {
    type: [Number, String],
    default: 60
  }
});

// 解析图片JSON字符串
const imageList = computed(() => {
  if (!props.images) return [];
  try {
    const parsed = JSON.parse(props.images);
    return Array.isArray(parsed) ? parsed : [];
  } catch (e) {
    console.warn('解析帖子图片JSON失败:', e);
    return [];
  }
});

// 处理图片URL
const processImageUrl = (url) => {
  if (!url) return '';
  // 如果是绝对路径（以 http(s):// 或 / 开头），直接返回
  if (/^(https?:)?\//.test(url)) {
    return url;
  }
  if (isExternal(url)) {
    return url;
  }
  return import.meta.env.VITE_APP_BASE_API + url;
};

// 第一张图片URL
const firstImageUrl = computed(() => {
  if (imageList.value.length === 0) return '';
  return processImageUrl(imageList.value[0]);
});

// 所有图片URL列表
const allImageUrls = computed(() => {
  return imageList.value.map(url => processImageUrl(url));
});

const realWidth = computed(() =>
  typeof props.width === 'string' ? props.width : `${props.width}px`
);

const realHeight = computed(() =>
  typeof props.height === 'string' ? props.height : `${props.height}px`
);
</script>

<style lang="scss" scoped>
.post-images-preview {
  position: relative;
  display: inline-block;
  
  .no-image {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: v-bind(realWidth);
    height: v-bind(realHeight);
    background-color: #f5f7fa;
    border: 1px dashed #dcdfe6;
    border-radius: 5px;
    color: #909399;
    font-size: 12px;
    
    .el-icon {
      font-size: 20px;
      margin-bottom: 4px;
    }
  }
  
  .post-image {
    border-radius: 5px;
    background-color: #ebeef5;
    box-shadow: 0 0 5px 1px #ccc;
    cursor: pointer;
    
    :deep(.el-image__inner) {
      transition: all 0.3s;
      
      &:hover {
        transform: scale(1.2);
      }
    }
    
    :deep(.image-slot) {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 100%;
      height: 100%;
      color: #909399;
      font-size: 20px;
    }
  }
  
  .image-count {
    position: absolute;
    bottom: 2px;
    right: 2px;
    background: rgba(0, 0, 0, 0.6);
    color: white;
    font-size: 10px;
    padding: 1px 4px;
    border-radius: 2px;
    line-height: 1;
  }
}
</style>